﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Exams;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Interface.Imitate;
using YAPACS.RisServer.Services.Dtos;
using YAPACS.RisServer.Services.MobileDtos;
using YAPACS.RisServer.Services.MobileParams;
using YAPACS.RisServer.Workflows.HomePage;

namespace YAPACS.RisServer.Controllers;

[Authorize]
public class MobileController(IRepository<Exam, Guid> examRepository,
                              IExamValidationService examValidationService,
                              IReportService reportService,
                              IReportExtendsService reportExtendsService,
                              IGetHomeAmisViewMobile getHomeAmisViewMobile
                             ) : Controller
{
    private readonly IRepository<Exam, Guid> _examRepository = examRepository;
    private readonly IExamValidationService _examValidationService = examValidationService;
    private readonly IReportService _reportService = reportService;
    private readonly IReportExtendsService _reportExtendsService = reportExtendsService;
    private readonly IGetHomeAmisViewMobile _getHomeAmisViewMobile = getHomeAmisViewMobile;
    /// <summary>
    /// 验证检查(手机端)
    /// </summary>
    /// <param name="examId"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    [HttpPost("api/exam/validate-mobile")]
    public async Task<IActionResult> ValidateMobile([FromBody] ExamValidateDto examValidateDto)
    {
        var exam = await _examRepository.GetAsync(examValidateDto.ExamId);
        var result = await _examValidationService.ValidateAsync(exam, examValidateDto.Type);
        if (result.Count > 0)
        {
            return Ok(new
            {
                success = true,
                data = result.OrderBy(r => r.Key).ToDictionary(r => r.Key.ToString(), r => r.Value)
            });
        }
        return Ok(new
        {
            success = true
        });
    }

    [HttpPost("api/report/save-mobile")]
    public async Task<IActionResult> SaveReportMobile([FromBody] ReportDto reportDto)
    {
        var result = await _reportService.SaveReportAsync(reportDto);
        return Ok(result);
    }

    // 移动端提交报告
    [HttpPost("api/report/submit-mobile")]
    public async Task<IActionResult> SubmitReportMobile([FromBody] ReportSubmitDto submitDto)
    {
        var result = await _reportService.SubmitReportAsync(submitDto.ReportId, submitDto.auditUserId, submitDto.Complete);
        return Ok(result);
    }

    /// <summary>
    /// 报告撤回(手机端)
    /// </summary>
    [HttpPost("api/report/revoke-mobile/{examId}")]
    public async Task<IActionResult> RevokeReportMobile(Guid examId, [FromBody] RevokeDto revokeDto)
    {
        var result = await _reportService.RevokeReportAsync(examId, revokeDto.ReviseType, revokeDto.Reason);
        return Ok(result);
    }

    /// <summary>
    /// 报告修正
    /// </summary>
    [HttpPost("api/report/amend-mobile/{examId}")]
    public async Task<IActionResult> AmendReportMobile(Guid examId, [FromBody] AmendParams amendParams)
    {
        var result = await _reportService.AmendReportAsync(examId, amendParams.ReviseType, amendParams.Reason);
        return Ok(result);
    }

    /// <summary>
    /// 驳回重写(移动端)
    /// </summary>
    [HttpPost("api/report/reject-mobile/{examId}")]
    public async Task<IActionResult> RejectReportMobile(Guid examId, [FromBody] AmendParams param)
    {
        var result = await _reportService.RejectReportAsync(examId, param.ReviseType, param.Reason);
        return Ok(result);
    }

    /// <summary>
    /// 放弃书写
    /// </summary>
    /// <param name="reportId"></param>
    /// <returns></returns>
    [HttpPost("api/report/give-up-write-report-mobile")]
    public async Task<IActionResult> GiveUpWriteReportMobile([FromBody] Guid reportId)
    {
        var result = await _reportService.GiveUpWriteReportAsync(reportId);
        return Ok(result);
    }

    /// <summary>
    /// 获取扩展字段
    /// </summary>
    /// <param name="reportId"></param>
    /// <returns></returns>
    [HttpGet("api/report/get-extend-fields/{reportOid}")]
    public async Task<IActionResult> GetExtendFieldsAsync(Guid reportOid)
    {
        var result = await _reportExtendsService.GetExtendFieldsAsync(reportOid);
        return Content(JsonConvert.SerializeObject(result),"application/json");
    }

    /// <summary>
    /// 获取amis schema
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home/get-home-view-mobile")]
    public async Task<IActionResult> GetAdvanceSearchView()
    {
        AmisRes res = new();
        var workFlowOutput = await _getHomeAmisViewMobile.InvokeAsync([]);
        res.Data = new { amisJson = workFlowOutput.View };
        return Json(res);
    }

    [HttpPost("api/report/get-list-mobile")]
    public async Task<IActionResult> GetExamListByFilter([FromBody] DataGridRequest request)
    {
        var result = await _reportService.GetExamListByFilterAsync(request);
        return Ok(result);
    }
}
